/*
自己选择的路 ,跪着也要走完。朋友们 , 虽然这个世界日益浮躁起来,只
要能够为了当时纯粹的梦想和感动坚持努力下去 , 不管其它人怎么样,我
们也能够保持自己的本色走下去。
To the world , you will be a person , but to a person , you
will be the world .                               ——AKPower
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
char s[1010];
int main()
{
    int T;
    int m;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%s%d", s, &m);
        if (m == 0)
        {
            printf("%s\n", s);
            continue;
        }
        int len = strlen(s);
        char minn = s[0], p = -1;
        for (int i = 1; i < len; i++)
        {
            if (s[i] == '0')
                continue;
            if (minn > s[i])
            {
                minn = s[i];
                p = i;
            }
        }
        if (p != -1)
        {
            m--;
            char tp = s[0];
            s[0] = s[p];
            s[p] = tp;
        }
        if (m == 0)
        {
            cout << s << endl;
            continue;
        }
        for (int i = 1; i < len; i++)
        {
            if (s[i] == '0')
                continue;
            char minn = s[i], p = i;
            for (int j = i + 1; j < len; j++)
            {
                if (minn > s[j])
                {
                    minn = s[j];
                    p = j;
                }
            }
            if (p != i)
            {
                m--;
                char tp = s[i];
                s[i] = s[p];
                s[p] = tp;
            }
            if (m == 0)
                break;
        }
        cout << s << endl;
    }

    getchar();
    getchar();
    return 0;
}
